<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="com.google.appengine.api.users.User"%>
<%@ page import="com.google.appengine.api.users.UserService"%>
<%@ page import="com.google.appengine.api.users.UserServiceFactory"%>
<%@ page import="javax.jdo.PersistenceManager"%>
<%@ page import="java.util.*"%>
<%@ page import="howl.types.*"%>
<%@ page import="howl.types.query.*"%>
<%@ page import="howl.model.*"%>
<%@ page import="howl.Twitter.EnableTwitter"%>
<%@ page import="javax.jdo.Query"%>
<%@page import="compiler.exceptions.HowlModelException"%>


<%!String getJs(String name) {
		try{
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		HowlUser hu = Model.getHowlUserfromUser(user);
		Model m = new Model(hu);

		TableDescriptor td = m.getTableDescriptor(name);
		ResultSet rs = QueryHelper.queryByEntity(name, 200);
		Hashtable<Date, Result> h = rs.getResults();
		ArrayList<Result> l = new ArrayList<Result>(h.values());
		String retval = "";

		List<DataProvider> Sensorresults = m.getDataProvider(td);
		ArrayList<DataProvider> sensors = new ArrayList<DataProvider>(
				Sensorresults);

		retval += "<script type=\"text/javascript\" src=\"http://www.google.com/jsapi\"></script>\n"
				+ "<script type=\"text/javascript\">\n"
				+ "google.load('visualization', '1', {packages: ['annotatedtimeline']});\n"
				+ "function drawVisualization() {\n"
				+ "var data = new google.visualization.DataTable();\n"
				+ "data.addColumn('date', 'Date');\n";

		Hashtable<String, Integer> t = new Hashtable<String, Integer>();
		int count = 1;
		for (int s = 0; s < sensors.size(); s++) {
			for (int i = 0; i < td.getStringmapper().size(); i++) {
				retval += "data.addColumn('number', '"
						+ sensors.get(s).getName() + "."
						+ td.getStringmapper().get(i) + "');\n";
				t.put(sensors.get(s).getName() + "."
						+ td.getStringmapper().get(i), Integer.valueOf(count));
				count++;
			}
		}

		retval += "data.addRows(" + l.size() + ");\n";

		for (int k = 0; k < l.size(); k++) {
			retval += "data.setValue(" + k + ", 0, new Date("
					+ (l.get(k).getTimestamp().getYear() + 1900) + ","
					+ (l.get(k).getTimestamp().getMonth()) + ","
					+ l.get(k).getTimestamp().getDate() + ","
					+ l.get(k).getTimestamp().getHours() + ","
					+ l.get(k).getTimestamp().getMinutes() + ","
					+ l.get(k).getTimestamp().getSeconds() + "));\n";

			for (int j = 0; j < td.getStringmapper().size(); j++) {
				item i = l.get(k).getMap().get(td.getStringmapper().get(j));

				if (i != null) {
					retval += "data.setValue("
							+ k
							+ ","
							+ t.get(l.get(k).getDp().getName() + "."
									+ i.getName()) + "," + i.getValue()
							+ ");\n";
				}
			}

		}

		retval += "var annotatedtimeline = new google.visualization.AnnotatedTimeLine("
				+ "document.getElementById('visualization'));\n annotatedtimeline.draw(data,"
				+ "{'displayAnnotations': true}); }\n"
				+ "google.setOnLoadCallback(drawVisualization) \n</script>\n";

		return retval;
	} catch (HowlModelException e){
		return null;
	}
}
	%>











</script>
